package bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class MemberShipDTO {
	
	private MemberShipVo vo;
	Connection conn = null;
	
	public MemberShipDTO() {			// 3. 생성자에서 conn 처리 
		conn = new MyOracle().getConn();
	}
	
	
//	public MemberShipDTO() {
//		try{
//			Context init = new InitialContext();
//			DataSource ds = (DataSource)init.lookup("java:comp/env/jdbc/OracleDB");
//			conn = ds.getConnection();
//			System.out.println("<h3>연결에 성공하였습니다</h3>");
//
//		}catch(Exception e){
//			System.out.println("<h3>연결에 실패하였습니다</h3>");
//			e.printStackTrace();
//		}
//	}
	
	
	
	
	
	public boolean insert() {
		boolean b = false;
		String sql = "insert into memberShip(mid,pwd,irum,bir,email,phone,address,answer,question)"
				+ " values(?,?,?,?,?,?,?,?,? )";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			pst.setString(2, vo.getPwd());
			pst.setString(3, vo.getIrum());
			pst.setString(4, vo.getBir());
			pst.setString(5, vo.getEmail());
			pst.setString(6, vo.getPhone());
			pst.setString(7, vo.getAddress());
			pst.setString(8, vo.getAnswer());
			pst.setString(9, vo.getQuestion());

			int cnt = pst.executeUpdate();
			if (cnt > 0) {
				b = true;
				
			}
			pst.close();
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}
	public boolean login() {
		boolean b = false;
		try {
			String sql = "select mid, pwd from membership where mid =? and pwd =?";
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getMid());
			System.out.println(vo.getMid());
			pst.setString(2, vo.getPwd());
			System.out.println(vo.getPwd());

			ResultSet rs = pst.executeQuery();
			if (rs.next())
				b = true;

			pst.close();
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}
	
	public ArrayList<MemberVo> select() {
		ArrayList<MemberVo> al = new ArrayList<>();
		String sql = "select * from membership where irum=?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, "%" + vo.getIrum() + "%");
			
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				MemberVo v = new MemberVo();
				v.setMid(rs.getString("mid"));
				v.setPwd(rs.getString("pwd"));
				v.setIrum(rs.getString("irum"));
				v.setPhoto(rs.getString("photo"));
				v.setBir(rs.getString("bir"));
				v.setAddress(rs.getString("address"));

				al.add(v);
			}
			rs.close();
			pst.close();
			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return al;
	}


	public MemberShipVo selectOne() {
		System.out.println("1");
		MemberShipVo v = new MemberShipVo();
		String sql = "select * from membership where irum=?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getIrum());//
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				v.setMid(rs.getString("mid"));
				System.out.println("1");
				v.setPwd(rs.getString("pwd"));
				v.setIrum(rs.getString("irum"));
				v.setPhone(rs.getString("phone"));
				v.setBir(rs.getString("bir"));
				v.setAddress(rs.getString("address"));
				v.setPhone(rs.getString("phone"));
			}
			rs.close();
			pst.close();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return v;
	}
	
	public MemberShipVo selecTwo() {
		System.out.println("1");
		MemberShipVo v = new MemberShipVo();
		String sql = "select * from membership where answer=?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getAnswer());//
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				v.setQuestion(rs.getString("question"));
				v.setAnswer(rs.getString("answer"));
				v.setPwd(rs.getString("pwd"));
				v.setMid(rs.getString("mid"));
			}
			rs.close();
			pst.close();
//			conn.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return v;
	}
	
	
	public boolean selectThree(){
		boolean flag= false;
		String sql = "select * from membership where question =? and answer =?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getQuestion());
			pst.setString(2, vo.getAnswer());
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				int cnt = pst.executeUpdate();
				flag=true;
				pst.close();
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return flag;
	}
	public boolean searchfour(){
		System.out.println(2);
		boolean f = false;
		String sql = "select * from membership where irum =? and phone =? and bir =?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, vo.getIrum());
			pst.setString(2, vo.getPhone());
			pst.setString(3, vo.getBir());
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				int cnt = pst.executeUpdate();
				f = true;
				pst.close();
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return f;
	}

	
	public boolean check(String mid){			// view를 위한 처리 
		System.out.println(mid);
		boolean flag = false;
		
		MemberShipVo vo = new MemberShipVo();
		String sql = "select * from membership where mid = ?";
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			pst.setString(1, mid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				
				if(rs.getString("mid").equals(mid)){
					flag = true;
					break;
				}
				else{
					flag = false;
				}
				//vo.setMid(rs.getString("mid"));
			}
			//if(vo.getMid().equals(null) || vo.getMid().equals("")){
			//}
			rs.close();
			pst.close();
			conn.close();
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return flag;
	}
	
	
	public MemberShipVo getVo() {
		return vo;
	}
	public void setVo(MemberShipVo vo) {
		this.vo = vo;
	}
	
	public Connection getConn() {
		return conn;
	}
	public void setConn(Connection conn) {
		this.conn = conn;
	}
	public static void main(String [] args){
		new MemberShipDTO();
	}
}
